<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Request;

class PortionTraveler extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    // 旅客组合数据
    protected $travellerInfos;

    protected function filter(&$map)
    {
        if ($this->request->param("driver_id")) {
            $map['driver_id'] = ["like", "%" . $this->request->param("driver_id") . "%"];
        }
        if ($this->request->param("driver_user_id")) {
            $map['driver_user_id'] = ["like", "%" . $this->request->param("driver_user_id") . "%"];
        }
        if ($this->request->param("group_id")) {
            $map['group_id'] = ["like", "%" . $this->request->param("group_id") . "%"];
        }
        if ($this->request->param("car_form")) {
            $map['car_form'] = ["like", "%" . $this->request->param("car_form") . "%"];
        }
        if ($this->request->param("remark")) {
            $map['remark'] = ["like", "%" . $this->request->param("remark") . "%"];
        }
    }

    /**
     * 未分配旅客
     * @return mixed
     */
    public function index(Request $request)
    {

        $data = input();
//        //搜索
        //订单ID
        if ($this->request->param("order_id")) {
            $map['o.order_id'] = array('like', "%{$this->request->param("order_id")}%");
        }

        //预订人
        if ($data['booking_people_name']) {
            $map['o.booking_people_name'] = array('like', "%{$data['booking_people_name']}%");
        }

        //预订人电话
        if ($data['booking_people_phone']) {
            $map['o.booking_people_phone'] = array('like', "%{$data['booking_people_phone']}%");
        }


        //平台
        if ($data['platform']) {
            $map['o.platform'] = $data['platform'];
        }

        //出行日期
        if ($data['start_time'] && $data['end_time']) {
            $map['o.start_time'] = array("between", [strtotime($data['start_time'] . " 00:00:00"), strtotime($data['end_time'] . " 23:59:59")]);
        } elseif ($data['start_time']) {
            $map['o.start_time'] = array("between", [strtotime($data['start_time'] . " 00:00:00"), strtotime($data['start_time'] . " 23:59:59")]);
        } elseif ($data['end_time']) {
            $map['o.start_time'] = array("between", [strtotime($data['end_time'] . " 00:00:00"), strtotime($data['end_time'] . " 23:59:59")]);
        }

        //预订人备注
        if ($data['booking_people_remark']) {
            $map['o.booking_people_remark'] = array('like', "%{$data['booking_people_remark']}%");
        }

        $listRows = $this->request->param('numPerPage') ?: config("paginate.list_rows");

//        $map['o.dispose_status'] = 'vehicle_dispatching';
        $map['ot.group_id'] = [">",0];
        $map['pt.isdeal'] = ["<>",0];
        $map['pt.isbooking'] = [">",0];
        $map['ot.is_error'] = 0;


        //查询订单信息
        $list = Db::name('order_traveler')
            ->alias("ot")
            ->join("order o", "o.id=ot.oid", 'left')
            ->join("portion_traveler pt", "o.id=pt.oid", 'left')
            ->where($map)
            ->where('ot.isaccommodation','包含住宿')
//            ->where(function ($query){
//                $query->whereNull('pt.isbooking')
//                    ->whereOr('pt.isbooking',0);
//            })
            ->group('ot.oid')
            ->order('ot.id desc')
            ->field("ot.*,o.*")
            ->paginate($listRows, false, ['query' => $this->request->get()]);
        $new_list = $list->toArray()['data'];
        foreach ($new_list as $key => $item) {
            if (empty($item['id'])) {
                unset($new_list[$key]);
            } else {
                $new_list[$key]['order_remark'] = Db::name('order_remark')
                    ->where('order_id', $item['order_id'])
                    ->order('id desc')
                    ->select();
                continue;
            }
        }

        $oids = [];
        $order_travelers = [];
        //整合订单ids
        foreach ($new_list as $item) {

            if ($oids[$item['order_id']] != true) {

                $travelers = $item;
                $arr = Db::name('order_traveler')->where('oid', $item['id'])->field('id')->select();
                $travelers['num'] = count($arr);
                $travelers['show_single'] = true;
                $travelers['show_mian'] = false;
                $travelers['show_between'] = false;
                $travelers['show_end'] = false;
                $travelers['route_title'] = Db::name('route')
                    ->where('id', $item['route_id'])
                    ->value('title');
                //获取该订单的旅客信息
                $travelers_info = Db::name('order_traveler')->where('oid', $item['id'])->select();
                $arr = [];
                foreach ($travelers_info as $k => $v) {
                    $arr[] = $v['id'] . '-' . $v['name'];
                }
                $info = implode(',', $arr);
                $travelers['travelers_info'] = $info;
                $travelers['travelers_info_list'] = $travelers_info;
                //查询该订单的关联订单id
                $relevance_id = Db::name('order')->where('id', $item['id'])->value('relevance_id');
                //获取订单ids
                $order_ids = Db::name('order_relevance')->where('relevance', $relevance_id)->field('order_id')->select();
                $relevance_order_list = [];
                foreach ($order_ids as $k => $v) {
                    if ($v['order_id'] != $item['id']) {
                        $relevance = Db::name('order')->where('id', $v['order_id'])->find();
                        //获取关联订单的旅客信息
                        $relevance_travelers_info = Db::name('order_traveler')->where('oid', $v['order_id'])->select();
                        $relevance_travelers_info = Db::name('order_traveler')->where('oid', $v['order_id'])->select();
                        $relevance['num'] = count($relevance_travelers_info);
                        $arrs = [];
                        foreach ($relevance_travelers_info as $kt => $vt) {
                            $arrs[] = $vt['id'] . '-' . $vt['name'];
                        }
                        $relevance_info = implode(',', $arrs);
                        $relevance['travelers_info'] = $relevance_info;
                        $relevance['travelers_info_list'] = $relevance_travelers_info;
                        $relevance['order_remark'] = Db::name('order_remark')
                            ->where('order_id', $v['order_id'])
                            ->order('id desc')
                            ->select();
                        $relevance_order_list[] = $relevance;
                    }
                }
                $travelers['relevance_order_list'] = $relevance_order_list;
                $order_travelers[] = $travelers;
                $oids[$item['order_id']] = true;

            }

        }

//        dump($order_travelers);
        // 模板赋值显示
        $platform = Db::name('platform')->select();
        $this->view->assign('Request.param.ctime', $data);
        $this->view->assign('Request.param.start_time', $data);
        $this->view->assign('platform', $platform);
        $this->view->assign('list', $order_travelers);
        if (!empty($order_travelers)) {
            $this->view->assign("count", count($order_travelers));
        }
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        if ($request->isAjax()) {
            $this->view->assign('refresh', 1);
            return $this->view->fetch('ajaxpage');
        } else {
            return $this->view->fetch('undistributed_traveller');
        }
    }

    /**
     * 分配住宿
     */
    public function deal()
    {
        $data = input();
        if (!$data['oid']){
            return ajax_return_adv_error('缺少参数');
        }
//        $portion_traveler = Db::name('portion_traveler')->where('oid', $data['oid'])->find();
        $order_traveler = Db::name('order_traveler')->where('oid', $data['oid'])->select();

        // 获取住宿派单
        $portionInfo = Db::name('portion_traveler')->where('oid', $data['oid'])->find();
        if (!$portionInfo){
            $insert = [
                'number_of_guests' => count($order_traveler),
                'oid' => $data['oid'],
                'car_form' => $portionInfo['car_form'],
                'create_time' => time(),
            ];
            // 添加到表里
            $addId = Db::name('portion_traveler')->strict(false)->insertGetId($insert);
            $portionInfo = Db::name('portion_traveler')->where('oid', $data['oid'])->find();
        }
        // 获取住宿旅客
        $travelerInfo = Db::name('order_traveler')
            ->where('oid',$data['oid'])
            ->where('isaccommodation','包含住宿')
            ->select();
        // 查询旅游路线
        foreach ($travelerInfo as $index => $item) {
            $route_id = Db::name('order')->where('id',$item['oid'])->value('route_id');
            $route_name = Db::name('route')->where('id',$route_id)->value('title');
            $travelerInfo[$index]['route_name'] = empty($route_name)?'旅游路线不存在':$route_name;
            $travelerInfo[$index]['remark'] = $item['accommodation'];
        }
        $count = count($travelerInfo);
        // 查询所有的酒店
        $hotel = Db::name('hotel')->where('isdelete',0)->select();
        $hotel_accommodations=Db::name('hotel_accommodation')->where('oid',$item['oid'])->select();
        $this->view->assign('hotel',$hotel);
        $this->view->assign('hotel_accommodations',$hotel_accommodations);
        $this->view->assign('count',$count);
        $this->view->assign('oid',$data['oid']);
        $this->view->assign('route_name',$route_name);
        $this->view->assign('info',$travelerInfo);
        return $this->view->fetch();
    }

    /**
     * 组合旅客
     */
    public function addGroup(Request $request)
    {
        $data = input();
        $filterArr = ['oid','user_id'];
        foreach ($data as $index => $datum) {
            if (!in_array($index,$filterArr)){
                unset($data[$index]);
            }
        }

        if (!$data['oid']){
            return ajax_return_adv_error('缺少必要的参数oid');
        }
        if (!$data['user_id']){
            return ajax_return_adv_error('缺少必要的参数user_id');
        }
        // 查询尚未组合的旅客的信息
//        $travelerInfos = Db::name('order_traveler')->where('id','in',$data['user_id'])->select();
        $allTravelerInfos = Db::name('order_traveler')->where('oid',$data['oid'])->select();
        $travelerInfos=$allTravelerInfos;
        $count = count($allTravelerInfos);
        if ($travelerInfos){
            // 查询旅游路线
            foreach ($travelerInfos as $index => $item) {
                $route_id = Db::name('order')->where('id',$item['oid'])->value('route_id');
                $route_name = Db::name('route')->where('id',$route_id)->value('title');
                $travelerInfos[$index]['route_name'] = empty($route_name)?'旅游路线不存在':$route_name;
                $travelerInfos[$index]['remark'] = $item['accommodation'];
            }
        }
        // 获取旅游路线
        $route_id = Db::name('order')->where('id',$data['oid'])->value('route_id');
        $route_title = Db::name('route')->where('id',$route_id)->value('title'); // 旅游名称
        if (!$route_title){
            $route_title = '旅游路线不存在';
        }
        // 查询组合姓名
        $travelers = Db::name('order_traveler')->where('id','in',implode(',',$data['user_id']))->field('id,name,accommodation')->select();
        $combine_name = '';
        $remark = '';
        foreach ($travelers as $index => $traveler) {
            $combine_name .= "{$traveler['id']}:{$traveler['name']} + ";
            $remark .= "{$traveler['id']}:{$traveler['accommodation']} + ";
        }
        if ($combine_name){
            $combine_name = rtrim($combine_name,'+ ');
        }
        if ($remark){
            $remark = rtrim($remark,'+ ');
        }
        // 组合的数据
        $countGroup = count($data['user_id']);// 获取选择用户的长度
        $groupData = [];
        if ($countGroup){
            $groupData = [
                'order_travel_ids' => implode(',',$data['user_id']),
                'route_id' => $route_title, //旅游名称
                'combine_name' => $combine_name,
                'remark' => $remark,
            ];
        }
        // 查询所有的酒店
        $hotel = Db::name('hotel')->where('isdelete',0)->select();
        $ret['info'] = $travelerInfos;
        $ret['hotel'] = $hotel;
        $ret['group'] = $groupData;
        $ret['type'] = gettype($groupData);
        $ret['count'] = $count;
        return ajax_return_adv('组合成功','','','','',$ret,5);
    }

    /**
     * 获取酒店的房型
     */
    public function getRoomType(Request $request)
    {
        $data = input();
        if (!$data['hotel_id']){
            return ajax_return_adv_error('缺少必要的参数');
        }
        // 获取当前酒店信息
        $hotel = Db::name('hotel')->where('id',$data['hotel_id'])->find();
        if ($hotel['isdelete'] == 1){
            return ajax_return_adv_error('当前的酒店已被删除，不能选择');
        }
        // 根据酒店的id获取房型
        $room_type = Db::name('room_hotel_number')->where('hotel_id',$data['hotel_id'])->select();
        if (!empty($room_type)){
            foreach ($room_type as $index => $item) {
                $room_name =  Db::name('hotel_room_type')->where('id',$item['room_type_id'])->value('room_name');
                $room_type[$index]['room_type_name'] = (empty($room_name)) ? '房型已被删除':$room_name;
            }
        }
        return ajax_return_adv('获取数据成功','','','','',$room_type,5);
    }
    /**
     * 获取酒店的房型
     */
    public function getRoomPrice(Request $request)
    {
        $data = input();
        if (!$data['hotel_id']){
            return ajax_return_adv_error('缺少必要的参数');
        }
        if (!$data['room_type_id']){
            return ajax_return_adv_error('缺少必要的参数');
        }
        if (!$data['check_in_date']){
            return ajax_return_adv_error('缺少必要的参数');
        }

        $maps['isdelete'] = ['<>',1];
        $maps['hotel_id'] = $data['hotel_id'];
        // 获取当前酒店信息
        $htpr = Db::name('hotel_time_price')->where($maps)->order('id','desc')->select();
        foreach ($htpr as $k =>$v) {
//                dump(strtotime($v['start_time']));
//                dump($data['check_in_date'][$i]);
            if(strtotime($v['start_time']) <= strtotime($data['check_in_date'])){
                $hotel_room_price= Db::name('hotel_room_price')->where('isdelete',0)->where('room_type_id',$data['room_type_id'])->find();
            }
        }

        if(empty($hotel_room_price)){
                return ajax_return_adv_error('不存在酒店协价信息');
        }
        return ajax_return_adv('获取数据成功','','','','',$hotel_room_price,5);
    }

    /**
     * 添加旅客数据
     */
    public function addTraveller()
    {
        $data = input();
//        dump($data);die('输出');
        // 过滤掉数据,不在这个数组的字段都过滤掉
        $filterArr = ['check_in_date','hotel_id','room_type_id','order_travel_ids','oid','id','unit_price','isbooking','isdeal'];
        $data['end_date']=$data['check_in_date'];
        foreach ($data as $index => $datum) {
            if (!in_array($index,$filterArr)){
                unset($data[$index]); // 销毁数据
                continue; // 跳出本次循环
            }
            if (!is_array($datum)){
                continue;
            }
            // 过滤字段为空的数据
            foreach ($datum as $index01 => $item01) {
                if (empty($item01)){
                   unset($data[$index][$index01]);
                }
            }
        }
        if (empty($data['order_travel_ids'])){
            return ajax_return_adv_error('缺少必要的参数');
        }else{
            // 检验派单住宿旅客是否已经分配住宿
            // 统计分配住宿派单的旅客的个数
            $idStr = '';
            foreach ($data['order_travel_ids'] as $index => $order_travel_id) {
                $idStr .= $order_travel_id. ',';
            }
            $idStr = rtrim($idStr,',');
            // 查询数据表的住宿旅客个数
            $order_travels = Db::name('order_traveler')
                ->where('oid',$data['oid'])
                ->where('isaccommodation','包含住宿')
                ->select();
            if ($idStr){
                if (count(explode(',',$idStr)) < count($order_travels)){
                    return ajax_return_adv_error('还有旅客没有分配住宿');
                }
            }
        }

        $portionInfo = Db::name('portion_traveler')->where('oid', $data['oid'])->find();
        // 检验是否已分配住宿
//        $isdeal = $portionInfo['isdeal'];
//        if ($isdeal){
//            return ajax_return_adv_error('住宿派单已经分配住宿');
//        }
        $len=count($data['order_travel_ids']);
        // 组装组合和信息
        $content = "您好,你的住宿信息是: ".PHP_EOL;
        for ($i = 0; $i < $len; $i++){
            //获取酒店信息
            $hotel = Db::name('hotel')->where('id',$data['hotel_id'][$i])->find();
            $hotel_name = $hotel['hotel_name'];
            $location = $hotel['hotel_location'];

//            if (strtotime($data['check_in_date'][$i]) > strtotime($data['end_date'][$i])){ // 入住日期不能大于退房日期
//                $group_count = $i + 1;
//                return ajax_return_adv_error("第{$group_count}组合入住日期不能大于退房日期");
//            }
//            $route_id = Db::name('order')->where('id',$data['oid'])->value('route_id');
            //查询单价(酒店协价)
//            $maps['isdelete'] = ['<>',1];
//            $maps['hotel_id'] = $data['hotel_id'][$i];
//            $maps['settlement_status'] = [''];
//            $htpr = Db::name('hotel_time_price')->where($maps)->order('id','desc')->select();

//            foreach ($htpr as $k =>$v) {
////                dump(strtotime($v['start_time']));
////                dump($data['check_in_date'][$i]);
//                if(strtotime($v['start_time']) <= strtotime($data['check_in_date'][$i]) && strtotime($v['end_time']) >= strtotime($data['end_date'][$i])){
//                    $unit_price = Db::name('hotel_room_price')->where('isdelete',0)->where('hotel_time_price_id',$v['id'])->value('room_price');
//                }
//            }
//
//            if(empty($unit_price)){
//                $unit_price=0;
////                return ajax_return_adv_error($hotel_name."在".$data['check_in_date'][$i]."至".$data['end_date'][$i].'时间内，不存在酒店协价信息');
//            }

            // 获取旅游路线
            $route_id = Db::name('order')->where('id',$data['oid'])->value('route_id');
            $route_title = Db::name('route')->where('id',$route_id)->value('title'); // 旅游名称
            if (!$route_title){
                $route_title = '旅游路线不存在';
            }

//            dump($data['order_travel_ids'][$i]);
//            exit(1);
            // 查询组合姓名
            $travelers = Db::name('order_traveler')->where('id','in',$data['order_travel_ids'][$i])->field('id,name,accommodation')->select();
            $combine_name = '';
            $remark = '';
            foreach ($travelers as $index => $traveler) {
                $combine_name .= "{$traveler['id']}:{$traveler['name']} + ";
                $remark .= "{$traveler['id']}:{$traveler['accommodation']} + ";
            }
            if ($combine_name){
                $combine_name = rtrim($combine_name,'+ ');
            }
            if ($remark){
                $remark = rtrim($remark,'+ ');
            }


            // 添加到住宿列表中的数据
            $insert[] = [
                'id'=>$data['id'][$i],
//                'driver_user_id'=>$group['driver_user_id'],
                'check_in_date'=>strtotime($data['check_in_date'][$i]),
                'end_date'=>strtotime($data['check_in_date'][$i]),
                'hotel_id'=>$data['hotel_id'][$i],
                'hotel_room_type'=>$data['room_type_id'][$i],
                'order_travel_ids'=>$data['order_travel_ids'][$i],
                'unit_price'=>$data['unit_price'][$i],
                'route_id'=>$route_id,
                'oid'=>$data['oid'],
                'create_time' => time(),
                'portion_traveler_id' => $portionInfo['id'],
                'total_price'=>$data['unit_price'][$i],
                'remark' => $remark,
                'name_of_passenger' => $combine_name,
            ];
            $travelers = Db::name('order_traveler')->where('id','in',$data['order_travel_ids'][$i])->select();

            if ($travelers){
                    $username = '';
                    $phone = ''; // 旅客手机号码
                    for ($j=0; $j < count($travelers); $j++) {
                        $username .= $travelers[$j]['name'] . '、';
                        $phone .= $travelers[$j]['phone'] . '、';
                    }
                    $type_name = Db::name('hotel_room_type')->where('id',$data['room_type_id'][$i])->value('room_name');
                    $username = rtrim($username,'、');
                    $phone = rtrim($phone,'、');
                    if (strlen($phone) < 5){
                        $phone = '';
                    }
                    $content .= "组合为:{$username},".PHP_EOL;
                    $content .= "联系电话为:{$phone},".PHP_EOL;
                    $content .= "他们的住宿时间为:{$data['check_in_date'][$i]}-{$data['end_date'][$i]},".PHP_EOL;
                    $content .= "酒店的名称为:{$hotel_name},".PHP_EOL;
                    $content .= "房间类型为:{$type_name},".PHP_EOL;
                    $content .= "酒店的地址为:{$location}。".PHP_EOL;
                 }
            }
        if (empty($insert)){
            $insert = [];
        }
        $getIds=[];
        foreach ($insert as $val){
            if($val['id']==0||!is_numeric($val['id'])){
                unset($val['id']);
                $getIds[] = Db::name('hotel_accommodation')->strict(false)->insertGetId($val); // 批量插入旅客住宿列表
            }else{
                $getIds[]=$val['id'];
                Db::name('hotel_accommodation')->strict(false)->where('id',$val['id'])->update($val);
            }
        }
        Db::name('hotel_accommodation')->where('oid',$data['oid'])->where('id','not in',$getIds)->delete();
        // 添加数据到表
//        $getIds = Db::name('hotel_accommodation')->strict(false)->insertAll($insert); // 批量插入旅客住宿列表
        if ($getIds){
            logs('addRecord',$insert);
            $this->travellerInfos = $insert;
            // 添加酒店的使用记录
            $this->addRecord();
            // 更改住宿派单状态
            Db::name('portion_traveler')->where('oid',$data['oid'])->update(['isdeal'=>$data['isdeal'],'isbooking'=>$data['isbooking']]);
//            // 发送短信给司机
//            $sendMsg = [
//                'user_id'=>$groupInfo['driver_user_id'],
//                'title'=> '旅客住宿安排',
//                'content'=>$content,
//                'dispatch_traveler_group_id'=>$data['group_id'][0],
//                'create_time'=>NOW_TIME,
//                'type'=>'system_ordinary_info',
//            ];
//            logs('sendMsg',$sendMsg);
//            logs('zhusupaidanxinxi',$content);
////            logs('zhusupaidanxinxi',strlen($content));
////            logs('zhusupaidanxinxi',mb_substr($content,0,300));
//            if (strlen($content) > 300){
//                $content = mb_substr($content,0,300);
//            }
//            Db::name('notify')->strict(false)->insert($sendMsg);
//            // 发送短息
//            umeng_send($groupInfo['driver_user_id'],'旅客住宿安排',$content);
            return ajax_return_adv('分配房间成功');
        }else{
            return ajax_return_adv_error('分配房间失败');
        }
    }

    /**
     * 添加酒店房间的使用记录
     */
    public function addRecord()
    {
        logs('zhusu',$this->travellerInfos);
        $data = $this->travellerInfos;
        foreach ($data as $index => $val) {
            $tian = round(($val['end_date']-$val['check_in_date'])/3600/24);
            // 当前的入住日期
            $check_in_date = date('Y-m-d',$val['check_in_date']);
            // 把入住日期与退房日期的相差天数
            $date = [$check_in_date];
            logs('cha',$date);
            for ($i=1; $i<= $tian; $i++){
                array_push($date,date("Y-m-d",strtotime("+{$i} day",strtotime("{$check_in_date}"))));//添加的
            }
            // 查找当前的酒店是否有住宿记录
            if ($date){
                foreach ($date as $index => $item) {
                    $find = Db::name('room_consumption')->where('hotel_id',$val['hotel_id'])->where('hotel_room_type_id',$val['hotel_room_type'])
                        ->where('date_of_stay',$item)->find();
                    if (!$find){
                        $insert = [
                            'hotel_id'=>$val['hotel_id'],
                            'hotel_room_type_id'=>$val['hotel_room_type'],
                            'date_of_stay'=>$item,
                            'create_time'=>time(),
                            'num'=>1,
                        ];
                        $addId = Db::name('room_consumption')->strict(false)->insertGetId($insert);
                        logs('zhusulog',$addId);
                    }else{
                        $num = $find['num']+1;
                        $up = Db::name('room_consumption')->where('hotel_id',$val['hotel_id'])->where('hotel_room_type_id',$val['hotel_room_type'])
                            ->where('date_of_stay',$item)->update(['num'=>$num]);
                        logs('upzhusulog', $up);
                    }
                }
            }
        }
    }

    /**
     * 获取旅客数据
     */
    public function getTraveller()
    {
        $data = input();
        if (!$data['id']){
            return ajax_return_adv_error('缺少参数',6);
        }
        // 查询旅客信息
        $travelerInfos = Db::name('order_traveler')->where('id','in',$data['id'])->select();
        $group_id = 0;
        if ($travelerInfos){
            // 查询旅游路线
            foreach ($travelerInfos as $index => $item) {
                $group_id = $item['group_id'];
                $route_id = Db::name('order')->where('id',$item['oid'])->value('route_id');
                $route_name = Db::name('route')->where('id',$route_id)->value('title');
                $travelerInfos[$index]['route_name'] = empty($route_name)?'旅游路线不存在':$route_name;
                $travelerInfos[$index]['remark'] = $item['accommodation'];
            }
        }
        $count = 0;
        if ($group_id){
            $travelerInfo = Db::name('order_traveler')
                ->where('group_id',$group_id)
                ->where('isaccommodation','包含住宿')
                ->select();
            $count = count($travelerInfo);
        }

        $ret['info'] = $travelerInfos;
        $ret['id'] = $data['id'];
        $ret['count'] = $count;
        return ajax_return_adv('取消组合成功','','','','',$ret,5);
    }

    /**
     * @title 获取添加的旅客信息
     * @author 邓汉炜
     * @url /admin/portion_traveler/getTravellerInfo
     */
    public function getTravellerInfo(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            if (!$postData['id']){
                return ajax_return_adv_error('缺少用户旅客必要信息');
            }
            // 根据传过来到的旅客id查找旅游信息
            $orderTraveler = Db::name('order_traveler')->where('id','in',$postData['id'])->select();
            $travellerGroup = [];
            if (!$orderTraveler){
                return ajax_return_adv_error('不存在该旅客信息');
            }else{
                foreach ($orderTraveler as $index => $item) {
                    $route = Db::name('order')->where('id',$item['oid'])->value('route_id');
                    $routeName = Db::name('route')->where('id',$route)->value('title');
                    $orderTraveler[$index]['route_name'] = empty($routeName)?'旅游路线不存在':$routeName;
                    $orderTraveler[$index]['remark'] = $item['accommodation'];
                }
            }
            $routeName = '';
            $user = '';
            $remark = '';
            $groupId = 0;
            foreach ($orderTraveler as $index01 => $item01) {
                $routeName = $item01['route_name'];
                $user .= "{$item01['id']}:{$item01['name']}+";
                $remark .= "{$item01['id']}:{$item01['remark']}+";
                $groupId = $item01['group_id'];
            }

            $travellerGroup['routeName'] = $routeName;
            $travellerGroup['group_id'] = $groupId;
            $travellerGroup['userName'] = rtrim($user,'+');
            $travellerGroup['order_travel_ids'] = $postData['id'];
            $travellerGroup['remark'] = rtrim($remark,'+');
            // 查找酒店
            $hotel = Db::name('hotel')->where('isdelete',0)->select();
            $result['orderTraveler'] = $travellerGroup;
            $result['hotel'] = $hotel;
            return ajax_return_adv('添加成功!','','','','',$result);
        }else{
            return ajax_return_adv_error('非法请求');
        }
    }

    /**
     * @title 已分配住宿
     * @author 邓汉炜
     * @url /admin/portion_traveler/allocated
     */
    public function allocated(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            $count = count($postData['groupIds']);
            // 过滤字段为空的参数
            foreach ($postData as $index01 => $postDatum02) {
                if ($index01 != 'portionId'){
                    foreach ($postDatum02 as $index03 => $item03) {
                        if (empty($item03)){
                            unset($postData[$index01][$index03]);
                        }
                    }
                }
            }
            foreach ($postData as $index => $postDatum) {
                if ($count != count($postDatum)){
                    switch ($index){
                        case 'check_in_date':
                            return ajax_return_adv_error('还有组合未选择入住日期'); break;
                        case 'end_date':
                            return ajax_return_adv_error('还有组合未选择退房日期'); break;
                        case 'hotel_id':
                            return ajax_return_adv_error('还有组合未选择酒店'); break;
                        case 'room_type_id':
                            return ajax_return_adv_error('还有组合未选择酒店房型'); break;
                    }
                }
            }
            $updateData = [];
            $driverUserId = 0;
            $groupId = $postData['groupIds'][0];
            if ($postData['portionId']){
                $driverGroup = Db::name('portion_traveler')->where('id',$postData['portionId'])->find();
                $driverUserId = $driverGroup['driver_user_id'];
            }
            for ($i = 0;$i < $count; $i++){
                if (strtotime($postData['check_in_date'][$i]) > strtotime($postData['end_date'][$i])){
                    return ajax_return_adv_error("第{$i}组合入住日期不能大于退房日期");
                }
                // 组装更新的数据
                $updateData[] = [
                    'id' => $postData['ids'][$i],
                    'group_id' => $postData['groupIds'][$i],
                    'check_in_date' => strtotime($postData['check_in_date'][$i]),
                    'end_date' => strtotime($postData['end_date'][$i]),
                    'hotel_id' => $postData['hotel_id'][$i],
                    'hotel_room_type' => $postData['room_type_id'][$i],
                    'portion_traveler_id' => $postData['portionId'],
                ];
            }
            // 获取之前的住房信息,删除之前的住房房间量,再添加新的房间量
            $hotelAccommodation = Db::name('hotel_accommodation')->where('portion_traveler_id',$postData['portionId'])->select();
            if ($hotelAccommodation){
                foreach ($hotelAccommodation as $index05  => $item05) {
                    $tian = round(($item05['end_date']-$item05['check_in_date'])/3600/24);
                    $date = [date('Y-m-d',$item05['check_in_date'])];
                    for ($i=1; $i<= $tian; $i++){
                        array_push($date,date("Y-m-d",strtotime("+{$i} day",$item05['check_in_date'])));//添加的
                    }
                    // 减少房间住宿量
                    foreach ($date as $index06 => $item06) {
                        $map['hotel_id'] = $item05['hotel_id'];
                        $map['hotel_room_type_id'] = $item05['hotel_room_type'];
                        $map['date_of_stay'] = $item06;
                        $findRoom = Db::name('room_consumption')->where($map)->find();
                        if (!$findRoom){
                            $insert = [
                                'hotel_id' => $item05['hotel_id'],
                                'hotel_room_type_id' => $item05['hotel_room_type'],
                                'date_of_stay' => $item06,
                                'create_time' => time(),
                                'num' => 0,
                            ];
                            Db::name('room_consumption')->strict(false)->insertGetId($insert);
                        }
                        // 自减
                        Db::name('room_consumption')->where($map)->setDec('num');
                    }
                }
            }
            // 增加新的房间使用量,并添加新的住宿记录
            if ($updateData){
                foreach ($updateData as $index => $updateDatum) {
                    $tian = round(($updateDatum['end_date']-$updateDatum['check_in_date'])/3600/24);
                    $date = [date('Y-m-d',$updateDatum['check_in_date'])];
                    for ($i=1; $i<= $tian; $i++){
                        array_push($date,date("Y-m-d",strtotime("+{$i} day",$updateDatum['check_in_date'])));//添加的
                    }
                    // 增加房间的使用量
                    foreach ($date as $index07 => $item07) {
                        $map['hotel_id'] = $updateDatum['hotel_id'];
                        $map['hotel_room_type_id'] = $updateDatum['hotel_room_type'];
                        $map['date_of_stay'] = $item07;
                        $findRoom = Db::name('room_consumption')->where($map)->find();
                        if (!$findRoom){ // 不存在
                            $insert = [
                                'hotel_id' => $updateDatum['hotel_id'],
                                'hotel_room_type_id' => $updateDatum['hotel_room_type'],
                                'date_of_stay' => $item07,
                                'create_time' => time(),
                                'num' => 0,
                            ];
                            Db::name('room_consumption')->strict(false)->insertGetId($insert);
                        }
                        // 自增
                        $up = Db::name('room_consumption')->where($map)->setInc('num');
                        logs('gengxinfangnum', $up);
                        if (!$up){
                            return ajax_return_adv_error('添加房间使用量失败');
                        }
                    }
                    // 更新旅客住宿信息
                    $updateTraveller = Db::name('hotel_accommodation')->strict(false)->update($updateDatum);
                    logs('gengxinlvke',$updateTraveller);
                }
                // 把旅客的住宿信息发送到司机app上
                if(!empty($driverUserId)){
                    $content = "您好,你的派单编号为{$groupId}的乘客住宿信息是: ".PHP_EOL;
                    // 查找所有旅客的住宿情况
                    $portionId = $postData['portionId'];
                    $portionData = Db::name('hotel_accommodation')->where('portion_traveler_id',$portionId)->select();
                    if ($portionData){
                        foreach ($portionData as $index => $portionDatum) {
                            $orderTravellerInfos = Db::name('order_traveler')->where('id','in',$portionDatum['order_travel_ids'])->select();
                            $username = '';
                            $phone = '';
                            if ($orderTravellerInfos){
                                $username = rtrim(implode(',',array_column($orderTravellerInfos,'name')),',');
                                $phone = rtrim(implode(',',array_column($orderTravellerInfos,'phone')),',');
                            }
                            $hotel = Db::name('hotel')->where('id',$portionDatum['hotel_id'])->find();
                            $hotel_name = empty($hotel['hotel_name']) ? '':$hotel['hotel_name'];
                            $location = empty($hotel['hotel_location']) ? '':$hotel['hotel_location'];
                            $type = Db::name('hotel_room_type')->where('id',$portionDatum['hotel_room_type'])->find();
                            $type_name = empty($type['room_name']) ? '':$type['room_name'];
                            $check_in_date = date('Y-m-d',$portionDatum['check_in_date']);
                            $end_date = date('Y-m-d',$portionDatum['end_date']);
                            $content .= "组合为:{$username},".PHP_EOL;
                            $content .= "联系电话为:{$phone},".PHP_EOL;
                            $content .= "他们的住宿时间为:{$check_in_date}-{$end_date},".PHP_EOL;
                            $content .= "酒店的名称为:{$hotel_name},".PHP_EOL;
                            $content .= "房间类型为:{$type_name},".PHP_EOL;
                            $content .= "酒店的地址为:{$location}。".PHP_EOL;
                        }
                    }
                    $sendMsg = [
                        'user_id'=>$driverUserId,
                        'title'=> '旅客住宿安排',
                        'content'=>$content,
                        'dispatch_traveler_group_id'=>$groupId,
                        'create_time'=>time(),
                        'type'=>'system_ordinary_info',
                    ];
                    logs('sendMsg',$sendMsg);
                    logs('zhusupaidanxinxi',$content);
                    if (strlen($content) > 300){
                        $content = mb_substr($content,0,300);
                    }
                    Db::name('notify')->strict(false)->insert($sendMsg);
                    umeng_send($driverUserId,'旅客住宿安排',$content);
                }
                return ajax_return_adv('更新成功');
            }
        }else{
            $data = input();
//            dump($data);
            if (!$data['id']) {
                return ajax_return_adv_error('缺少必要的参数');
            }
            $id = $data['id'];
            // 根据派单id查找住宿信息
            $portionInfo = Db::name('portion_traveler')->where('id',$data['id'])->find();
            if (!$portionInfo){
                return ajax_return_adv_error('不存在该住宿派单信息');
            }
            // 从住宿列表查找相关信息
            $hotelAccommodation = Db::name('hotel_accommodation')->where('portion_traveler_id',$portionInfo['id'])->select();
            $room_types = [];
            if ($hotelAccommodation){
                foreach ($hotelAccommodation as $index => $item) {
                    $hotelAccommodation[$index]['realname'] = Db::name('admin_user')->where('id',$item['driver_user_id'])->value('realname');
                    $hotelAccommodation[$index]['check_in_date'] = empty($item['check_in_date'])?'':date('Y-m-d',$item['check_in_date']);
                    $hotelAccommodation[$index]['end_date'] = empty($item['end_date'])?'':date('Y-m-d',$item['end_date']);
                    $orderTravellers = Db::name('order_traveler')->where('id','in',$item['order_travel_ids'])->field('id,name,accommodation')->select();
                    $userName = '';
                    $remark = '';
                    if ($orderTravellers){
                        foreach ($orderTravellers as $index01 => $orderTraveller) {
                            $userName .= "{$orderTraveller['id']}:{$orderTraveller['name']}+";
                            $remark .= "{$orderTraveller['id']}:{$orderTraveller['accommodation']}+";
                        }
                    }
                    $hotelAccommodation[$index]['name'] = rtrim($userName,'+');
                    $hotelAccommodation[$index]['remark'] = rtrim($remark,'+');
                    // 查找路线名称
                    $routeName = Db::name('route')->where('id',$item['route_id'])->value('title');
                    $hotelAccommodation[$index]['route_name'] = empty($routeName)? '该旅游路线不存在':$routeName;
                    // 根据酒店id查找当前酒店的所有房型
                    $roomTypes = Db::name('room_hotel_number')->where('hotel_id',$item['hotel_id'])->select();
                    if ($roomTypes){
                        foreach ($roomTypes as $index02 => $roomType) {
                            $routeName = Db::name('hotel_room_type')->where('id',$roomType['room_type_id'])->value('room_name');
                            $roomTypes[$index02]['room_name'] = empty($routeName) ? '房型不存在':$routeName;
                        }
                    }
                    $hotelAccommodation[$index]['roomTypes'] = $roomTypes;
                }
            }
            // 查找所有酒店
            $hotel = Db::name('hotel')->field('id,hotel_name')->select();
//            dump($hotelAccommodation);
            return view('',compact('hotelAccommodation','hotel','roomTypes','id'));
        }
    }

    /**
     * @title 获取酒店旅客信息
     * @url /admin/portion_traveler/getInfo
     */
    public function getInfo()
    {
        $data = input();
        // 根据派单id查找住宿信息
        $portionInfo = Db::name('portion_traveler')->where('id',$data['id'])->find();
        if (!$portionInfo){
            return ajax_return_adv_error('不存在该住宿派单信息');
        }
        // 从住宿列表查找相关信息
        $hotelAccommodation = Db::name('hotel_accommodation')->where('portion_traveler_id',$portionInfo['id'])->select();
        if ($hotelAccommodation){
            foreach ($hotelAccommodation as $index => $item) {
                $hotelAccommodation[$index]['realname'] = Db::name('admin_user')->where('id',$item['driver_user_id'])->value('realname');
                $hotelAccommodation[$index]['check_in_date'] = empty($item['check_in_date'])?'':date('Y-m-d',$item['check_in_date']);
                $hotelAccommodation[$index]['end_date'] = empty($item['end_date'])?'':date('Y-m-d',$item['end_date']);
                $orderTravellers = Db::name('order_traveler')->where('id','in',$item['order_travel_ids'])->field('id,name,accommodation')->select();
                $userName = '';
                $remark = '';
                if ($orderTravellers){
                    foreach ($orderTravellers as $index01 => $orderTraveller) {
                        $userName .= "{$orderTraveller['id']}:{$orderTraveller['name']}+";
                        $remark .= "{$orderTraveller['id']}:{$orderTraveller['accommodation']}+";
                    }
                }
                $hotelAccommodation[$index]['name'] = rtrim($userName,'+');
                $hotelAccommodation[$index]['remark'] = rtrim($remark,'+');
                // 查找路线名称
                $routeName = Db::name('route')->where('id',$item['route_id'])->value('title');
                $hotelAccommodation[$index]['route_name'] = empty($routeName)? '该旅游路线不存在':$routeName;
                // 根据酒店id查找当前酒店的所有房型
                $roomTypes = Db::name('room_hotel_number')->where('hotel_id',$item['hotel_id'])->select();
                if ($roomTypes){
                    foreach ($roomTypes as $index02 => $roomType) {
                        $routeName = Db::name('hotel_room_type')->where('id',$roomType['room_type_id'])->value('room_name');
                        $roomTypes[$index02]['room_name'] = empty($routeName) ? '房型不存在':$routeName;
                    }
                }
            }
        }
        // 查找所有酒店
        $hotel = Db::name('hotel')->field('id,hotel_name')->select();
        $result['hotelAccommodation'] = $hotelAccommodation;
        $result['roomTypes'] = $roomTypes;
        $result['hotel'] = $hotel;
        return ajax_return_adv('获取数据成功', '','','','',$result);
    }

    /**司机详细资料**/
    public function details()
    {
        $data = input();

        $vo = Db::name('driver')->where('user_id', $data['driver_user_id'])->find();
        if (!$vo){
            return ajax_return_adv_error('不存在该司机信息');
        }
        $users = Db::name('admin_user')->where('id',$vo['user_id'])->find();
        if ($users['id']){
            $users['user_id'] = $users['id'];
            unset($users['id']);
        }
        if (!$users){
            return ajax_return_adv_error('不存在该司机的用户信息',1);
        }
        //查询司机等级
        $driverRank = Db::name('driver_rank')->where('id',$vo['driver_rank_id'])->find();
        if ($driverRank){
            $vo['driver_rank_id'] = $driverRank['name'];
        }else{
            $vo['driver_rank_id'] = '新手';
        }
        $this->view->assign('users', $users);
        $this->view->assign('vo', $vo);
        return $this->view->fetch('details');
    }

    /**
     * 未分配住宿派单
     * 李正
     */
    public function unputupdis()
    {
        $data = input();
        $map['isdeal'] = 0;
        // 查询条件
        //路线
        if ($data['title']){
            $route = Db::name('route')->where('title','like',"%{$data['title']}%")->field('id')->select();
            foreach ($route as $k => $v)
            {
                $rid[] = $v['id'];
            }
            $dis = Db::name('dispatch_traveler_group')->where('route_id','in',$rid)->field('id')->select();
            foreach($dis as $k => $v)
            {
                $dis_id[] = $v['id'];
            }
            $map['group_id'] = ['in',$dis_id];
        }

        //派单号
        if ($data['group_id']){
                $map['group_id'] = $data['group_id'];

        }
        //司机名称
        if ($data['driver_user_id']){
            $userInfos = Db::name('admin_user')->where('realname','like',"%{$data['driver_user_id']}%")
                ->field('id')->select();
            $userIds = '';
            if ($userInfos){
                foreach ($userInfos as $index => $userInfo) {
                    $userIds .= $userInfo['id'].',';
                }
                $map['driver_user_id'] = ['in',rtrim($userIds,',')];
            }
        }
        //客人名称
        if($data['uname']){
            $otr = Db::name('order_traveler')->where('name','like',"%{$data['uname']}%")->field('group_id')->select();
            foreach($otr as $k => $v) {
                $group_id[] = $v['group_id'];
            }
            $map['group_id'] = ['in',$group_id];
        }
        //手机号码
        if($data['phone']){
            $otr = Db::name('order_traveler')->where('phone',$data['phone'])->field('group_id')->select();
            foreach($otr as $k => $v) {
                $group_idp[] = $v['group_id'];
            }
            $map['group_id'] = ['in',$group_idp];
        }
        //出发日期
        //出行日期
        if($data['departure_date'] && $data['finish_date']){
            $map['departure_date'] = array("between",[strtotime($data['departure_date']." 00:00:00"),strtotime($data['finish_date']." 23:59:59")]);
        }elseif($data['departure_date']){
            $map['departure_date'] = array("between",[strtotime($data['departure_date']." 00:00:00"),strtotime($data['departure_date']." 23:59:59")]);
        }elseif($data['finish_date']){
            $map['finish_date'] = array("between",[strtotime($data['finish_date']." 00:00:00"),strtotime($data['finish_date']." 23:59:59")]);
        }

        if ($data['car_form']){
            $map['car_form'] = ['like',"%{$data['car_form']}%"];
        }
        // 查询所有的派单数据
        if ($this::$isdelete){
            $map['isdelete'] = 1;
        }else{
            $map['isdelete'] = 0;
        }
        $listRows = $this->request->param('numPerPage') ?20: config("paginate.list_rows");
        $list = Db::name('portion_traveler')->where($map)->order('id','desc')->paginate($listRows, false, ['query' => $this->request->get()]);
        $count = $list->total();
        $this->view->assign('unp',1);
        $this->view->assign('count',$count);
        $this->view->assign('list',$list);
        $this->view->assign('page',$list->render());
        return $this->view->fetch();
    }

    /**
     * 导出数据到excel
     */
    public function get_excel(Request $request)
    {
        $data = input();
        Vendor('PHPExcel.PHPExcel');
        Vendor('PHPExcel.PHPExcel.WorkSheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        //实例化
        $export_excel = new \PHPExcel();
        //设置文档属性
        $writer = \PHPExcel_IOFactory::createWriter($export_excel,'Excel2007');
        //设置内容
        $export_sheet = $export_excel->getActiveSheet();
        for ($l = 0; $l < 26; $l++) {
            $letter[] = chr($l+65);
        }
        $countChr = count($letter);
        for ($j = 0; $j < $countChr; $j++) {
            for ($k = 0; $k < $countChr; $k++) {
                $letter[] = $letter[$j].$letter[$k];
            }
        }
        $tableHeader = ['路线名称','派单车号','司机','客人数量','出发日期','结束日期','备注'];
        $li_length = count($letter);//设置单元格的宽度
        for ($li=0;$li<$li_length;$li++){
            switch ($letter[$li]){
                case 'A':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'B':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'C':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'D':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'E':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'G':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'H':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'I':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'J':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'K':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                case 'L':$export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);break;
                default:
                    $export_sheet->getColumnDimension("$letter[$li]")->setWidth(25);
            }
        }
        $tableLength = count($tableHeader);
        for ($i = 0; $i < $tableLength; $i++){
            $export_sheet->setCellValue("$letter[$i]1","$tableHeader[$i]");
        }

        //查询未分配住宿派单
        $map['isdeal'] = 0;
        $map['id'] = ['in',$data['ids']];
        //查询要填充表格数据
        $lists = Db::name('portion_traveler')
            ->where($map)
            ->order("id desc")
            ->select();

//        dump($list);die;
        //字段转换
        foreach ($lists as $lk=>$lv) {
            if(!empty($lv['group_id'])){
                $route_id = Db::name('dispatch_traveler_group')->where('id',$lv['group_id'])->value('route_id');
                $title =  Db::name('route')->where('id',$route_id)->value('title');
            }
            $user_name = Db::name('admin_user')->where('id',$lv['driver_user_id'])->value('realname');
            $list[$lk]['title'] = empty($title) ? '' : $title; //路线名称
            $list[$lk]['group_id'] = empty($lv['group_id']) ? '' : $lv['group_id']; //派车单号
            $list[$lk]['user_name'] = empty($user_name) ? '' : $user_name; //司机
            $list[$lk]['number_of_guests'] = empty($lv['number_of_guests']) ? 0 : $lv['number_of_guests']; //客人数量
            $list[$lk]['departure_date'] = empty($lv['departure_date']) ? '' : date('Y-m-d',$lv['departure_date']); //出发时间
            $list[$lk]['finish_date'] = empty($lv['finish_date']) ? '' : date('Y-m-d',$lv['finish_date']); //结束时间
            $list[$lk]['remark'] = empty($lv['remark']) ? '' : $lv['remark']; //备注
        }
        //定义table键
        if (!empty($list)){
            $tableKey = array_keys($list[0]);
            //填充表格信息
            $tableKeyLength = count($tableKey);
            foreach ($list as $tk=>$tv){
                $tk += 2;
                for ($i = 0; $i < $tableKeyLength; $i++){
                    $export_sheet->setCellValue("$letter[$i]".$tk,$tv[$tableKey[$i]]);
                }
            }
        }
        $fileName = date('YmdHis',time())."酒店管理-未分配住宿旅客数据.xls";
        $fileName = iconv('utf-8','gb2312',$fileName);
        ob_end_clean();
        if ($request->isGet()){
            header('Content-Type:application/force-download');
            header('Content-Type:application/octet-stream');
            header('Content-Type:application/download');
            header('Content-Disposition:inline;filename='.$fileName);
            header('Content-Transfer-Encoding:binary');
            header('Cache-Control:must-revalidate,post-check=0');
            header('Pragma:no-cache');
            $writer->save('php://output');
            exit();
        }else{
            // 保存文件到服务器上
            $path = ROOT_PATH."public/excel";
            // 判断文件是否存在
            if (!file_exists($path)){
                mkdir($path,0777,true);
            }
            $fileName = iconv('gb2312','utf-8',$fileName);
            $file = $path."/{$fileName}";
            $writer->save($file);
            $url =$request->domain()."/excel/{$fileName}";
            return ajax_return_adv($url,'','','','','',5);
        }
    }

}
